
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.core.signing &#8212; Django 1.11.22.dev20190603194737 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.11.22.dev20190603194737 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-core-signing">
            
  <h1>Source code for django.core.signing</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Functions for creating and restoring url-safe signed JSON objects.</span>

<span class="sd">The format used looks like this:</span>

<span class="sd">&gt;&gt;&gt; signing.dumps(&quot;hello&quot;)</span>
<span class="sd">&#39;ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk&#39;</span>

<span class="sd">There are two components here, separated by a &#39;:&#39;. The first component is a</span>
<span class="sd">URLsafe base64 encoded JSON of the object passed to dumps(). The second</span>
<span class="sd">component is a base64 encoded hmac/SHA1 hash of &quot;$first_component:$secret&quot;</span>

<span class="sd">signing.loads(s) checks the signature and returns the deserialized object.</span>
<span class="sd">If the signature fails, a BadSignature exception is raised.</span>

<span class="sd">&gt;&gt;&gt; signing.loads(&quot;ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk&quot;)</span>
<span class="sd">u&#39;hello&#39;</span>
<span class="sd">&gt;&gt;&gt; signing.loads(&quot;ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified&quot;)</span>
<span class="sd">...</span>
<span class="sd">BadSignature: Signature failed: ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified</span>

<span class="sd">You can optionally compress the JSON prior to base64 encoding it to save</span>
<span class="sd">space, using the compress=True argument. This checks if compression actually</span>
<span class="sd">helps and only applies compression if the result is a shorter string:</span>

<span class="sd">&gt;&gt;&gt; signing.dumps(range(1, 20), compress=True)</span>
<span class="sd">&#39;.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ&#39;</span>

<span class="sd">The fact that the string is compressed is signalled by the prefixed &#39;.&#39; at the</span>
<span class="sd">start of the base64 JSON.</span>

<span class="sd">There are 65 url-safe characters: the 64 used by url-safe base64 and the &#39;:&#39;.</span>
<span class="sd">These functions make use of all of them.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">base64</span>
<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">zlib</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">baseconv</span>
<span class="kn">from</span> <span class="nn">django.utils.crypto</span> <span class="k">import</span> <span class="n">constant_time_compare</span><span class="p">,</span> <span class="n">salted_hmac</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="k">import</span> <span class="n">force_bytes</span><span class="p">,</span> <span class="n">force_str</span><span class="p">,</span> <span class="n">force_text</span>
<span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="k">import</span> <span class="n">import_string</span>

<span class="n">_SEP_UNSAFE</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^[A-z0-9-_=]*$&#39;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">BadSignature</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Signature does not match</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="k">class</span> <span class="nc">SignatureExpired</span><span class="p">(</span><span class="n">BadSignature</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Signature timestamp is older than required max_age</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="k">def</span> <span class="nf">b64_encode</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">urlsafe_b64encode</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;=&#39;</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">b64_decode</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">pad</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;=&#39;</span> <span class="o">*</span> <span class="p">(</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="o">%</span> <span class="mi">4</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">base64</span><span class="o">.</span><span class="n">urlsafe_b64decode</span><span class="p">(</span><span class="n">s</span> <span class="o">+</span> <span class="n">pad</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">base64_hmac</span><span class="p">(</span><span class="n">salt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">b64_encode</span><span class="p">(</span><span class="n">salted_hmac</span><span class="p">(</span><span class="n">salt</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">key</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">get_cookie_signer</span><span class="p">(</span><span class="n">salt</span><span class="o">=</span><span class="s1">&#39;django.core.signing.get_cookie_signer&#39;</span><span class="p">):</span>
    <span class="n">Signer</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">SIGNING_BACKEND</span><span class="p">)</span>
    <span class="n">key</span> <span class="o">=</span> <span class="n">force_bytes</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">SECRET_KEY</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">Signer</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;django.http.cookies&#39;</span> <span class="o">+</span> <span class="n">key</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="n">salt</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">JSONSerializer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Simple wrapper around json to be used in signing.dumps and</span>
<span class="sd">    signing.loads.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">dumps</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">separators</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;latin-1&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">loads</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;latin-1&#39;</span><span class="p">))</span>


<div class="viewcode-block" id="dumps"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.dumps">[docs]</a><span class="k">def</span> <span class="nf">dumps</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="s1">&#39;django.core.signing&#39;</span><span class="p">,</span> <span class="n">serializer</span><span class="o">=</span><span class="n">JSONSerializer</span><span class="p">,</span> <span class="n">compress</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns URL-safe, sha1 signed base64 compressed JSON string. If key is</span>
<span class="sd">    None, settings.SECRET_KEY is used instead.</span>

<span class="sd">    If compress is True (not the default) checks if compressing using zlib can</span>
<span class="sd">    save some space. Prepends a &#39;.&#39; to signify compression. This is included</span>
<span class="sd">    in the signature, to protect against zip bombs.</span>

<span class="sd">    Salt can be used to namespace the hash, so that a signed string is</span>
<span class="sd">    only valid for a given namespace. Leaving this at the default</span>
<span class="sd">    value or re-using a salt value across different parts of your</span>
<span class="sd">    application without good cause is a security risk.</span>

<span class="sd">    The serializer is expected to return a bytestring.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">serializer</span><span class="p">()</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="c1"># Flag for if it&#39;s been compressed or not</span>
    <span class="n">is_compressed</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">if</span> <span class="n">compress</span><span class="p">:</span>
        <span class="c1"># Avoid zlib dependency unless compress is being used</span>
        <span class="n">compressed</span> <span class="o">=</span> <span class="n">zlib</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">compressed</span><span class="p">)</span> <span class="o">&lt;</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">compressed</span>
            <span class="n">is_compressed</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">base64d</span> <span class="o">=</span> <span class="n">b64_encode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">is_compressed</span><span class="p">:</span>
        <span class="n">base64d</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;.&#39;</span> <span class="o">+</span> <span class="n">base64d</span>
    <span class="k">return</span> <span class="n">TimestampSigner</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="n">salt</span><span class="p">)</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">base64d</span><span class="p">)</span></div>


<div class="viewcode-block" id="loads"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.loads">[docs]</a><span class="k">def</span> <span class="nf">loads</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="s1">&#39;django.core.signing&#39;</span><span class="p">,</span> <span class="n">serializer</span><span class="o">=</span><span class="n">JSONSerializer</span><span class="p">,</span> <span class="n">max_age</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Reverse of dumps(), raises BadSignature if signature fails.</span>

<span class="sd">    The serializer is expected to accept a bytestring.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># TimestampSigner.unsign always returns unicode but base64 and zlib</span>
    <span class="c1"># compression operate on bytes.</span>
    <span class="n">base64d</span> <span class="o">=</span> <span class="n">force_bytes</span><span class="p">(</span><span class="n">TimestampSigner</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="n">salt</span><span class="p">)</span><span class="o">.</span><span class="n">unsign</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">max_age</span><span class="o">=</span><span class="n">max_age</span><span class="p">))</span>
    <span class="n">decompress</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">if</span> <span class="n">base64d</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">&#39;.&#39;</span><span class="p">:</span>
        <span class="c1"># It&#39;s compressed; uncompress it first</span>
        <span class="n">base64d</span> <span class="o">=</span> <span class="n">base64d</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">decompress</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">b64_decode</span><span class="p">(</span><span class="n">base64d</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">decompress</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">zlib</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">serializer</span><span class="p">()</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">data</span><span class="p">)</span></div>


<div class="viewcode-block" id="Signer"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.Signer">[docs]</a><span class="k">class</span> <span class="nc">Signer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39;:&#39;</span><span class="p">,</span> <span class="n">salt</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># Use of native strings in all versions of Python</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">key</span> <span class="o">=</span> <span class="n">key</span> <span class="ow">or</span> <span class="n">settings</span><span class="o">.</span><span class="n">SECRET_KEY</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sep</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">_SEP_UNSAFE</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s1">&#39;Unsafe Signer separator: </span><span class="si">%r</span><span class="s1"> (cannot be empty or consist of &#39;</span>
                <span class="s1">&#39;only A-z0-9-_=)&#39;</span> <span class="o">%</span> <span class="n">sep</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">salt</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span><span class="n">salt</span> <span class="ow">or</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__module__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">signature</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">signature</span> <span class="o">=</span> <span class="n">base64_hmac</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">salt</span> <span class="o">+</span> <span class="s1">&#39;signer&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">key</span><span class="p">)</span>
        <span class="c1"># Convert the signature from bytes to str only on Python 3</span>
        <span class="k">return</span> <span class="n">force_str</span><span class="p">(</span><span class="n">signature</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s%s%s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">unsign</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">signed_value</span><span class="p">):</span>
        <span class="n">signed_value</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span><span class="n">signed_value</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">signed_value</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">BadSignature</span><span class="p">(</span><span class="s1">&#39;No &quot;</span><span class="si">%s</span><span class="s1">&quot; found in value&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">)</span>
        <span class="n">value</span><span class="p">,</span> <span class="n">sig</span> <span class="o">=</span> <span class="n">signed_value</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">constant_time_compare</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">value</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">raise</span> <span class="n">BadSignature</span><span class="p">(</span><span class="s1">&#39;Signature &quot;</span><span class="si">%s</span><span class="s1">&quot; does not match&#39;</span> <span class="o">%</span> <span class="n">sig</span><span class="p">)</span></div>


<div class="viewcode-block" id="TimestampSigner"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.TimestampSigner">[docs]</a><span class="k">class</span> <span class="nc">TimestampSigner</span><span class="p">(</span><span class="n">Signer</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">timestamp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">baseconv</span><span class="o">.</span><span class="n">base62</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>

<div class="viewcode-block" id="TimestampSigner.sign"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.TimestampSigner.sign">[docs]</a>    <span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">force_str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s%s%s</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">timestamp</span><span class="p">())</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">TimestampSigner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">sign</span><span class="p">(</span><span class="n">value</span><span class="p">)</span></div>

<div class="viewcode-block" id="TimestampSigner.unsign"><a class="viewcode-back" href="../../../topics/signing.html#django.core.signing.TimestampSigner.unsign">[docs]</a>    <span class="k">def</span> <span class="nf">unsign</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">max_age</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Retrieve original value and check it wasn&#39;t signed more</span>
<span class="sd">        than max_age seconds ago.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">result</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">TimestampSigner</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">unsign</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="n">value</span><span class="p">,</span> <span class="n">timestamp</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sep</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">timestamp</span> <span class="o">=</span> <span class="n">baseconv</span><span class="o">.</span><span class="n">base62</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">timestamp</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">max_age</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">max_age</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">):</span>
                <span class="n">max_age</span> <span class="o">=</span> <span class="n">max_age</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
            <span class="c1"># Check timestamp is not older than max_age</span>
            <span class="n">age</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">timestamp</span>
            <span class="k">if</span> <span class="n">age</span> <span class="o">&gt;</span> <span class="n">max_age</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">SignatureExpired</span><span class="p">(</span>
                    <span class="s1">&#39;Signature age </span><span class="si">%s</span><span class="s1"> &gt; </span><span class="si">%s</span><span class="s1"> seconds&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">age</span><span class="p">,</span> <span class="n">max_age</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">value</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Jun 03, 2019</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>